package com.example.neteasecloudmusic.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.example.neteasecloudmusic.R;

public class PlayMusicView extends FrameLayout {

    private Context mContext;
    private View mView;
    private FrameLayout mFlPlayMusic;
    private ImageView mIvIcon, mIvNeedle;
    private Animation mPlayMusicAnim, mPlayNeedleAnim, mStopNeedleAnim;


    public PlayMusicView(@NonNull Context context) {
        super(context);
        init(context);
    }

    public PlayMusicView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PlayMusicView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public PlayMusicView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    /**
     * init playMusicView
     * @param context
     */
    private void init (Context context) {
        mContext = context;
        mView = LayoutInflater.from(context).inflate(R.layout.play_music_ani, this, false);
        mFlPlayMusic = mView.findViewById(R.id.fl_play_music);

        mIvIcon = mView.findViewById(R.id.play_nested_img);
        mIvNeedle = mView.findViewById(R.id.play_needle);
        mPlayMusicAnim = AnimationUtils.loadAnimation(mContext, R.anim.play_music_anim);
        mPlayNeedleAnim = AnimationUtils.loadAnimation(mContext, R.anim.play_needle_anim);
        mStopNeedleAnim = AnimationUtils.loadAnimation(mContext, R.anim.stop_needle_anim);

        addView(mView);
    }

    /**
     * play music
     */
    public void playMusic () {
        mFlPlayMusic.startAnimation(mPlayMusicAnim);
        mIvNeedle.startAnimation(mPlayNeedleAnim);
    }

    public void stopMusic () {
        mFlPlayMusic.clearAnimation();
        mIvNeedle.startAnimation(mStopNeedleAnim);
    }

    /**
     * set the song's image in disc center
     */
    public void setMusicIcon (String icon) {
        Glide.with(mContext)
                .load(icon)
                .into(mIvIcon);
    }


}




























